package com.hotel.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import com.hotel.bean.Regist;
import com.hotel.dao.UserDao;
import com.hotel.util.DBUtils;

public class UserDaoImpl implements UserDao {


	@Override
	public boolean insert(Regist t) throws SQLException {
		Connection conn = DBUtils.getConnection();
		String sql = "insert into regist values(seq.nextval, ?, ?, ?)";
		PreparedStatement psmt = conn.prepareStatement(sql);
		psmt.setString(1, t.getPname());
		psmt.setInt(2, t.getPmoblie());
		psmt.setString(3,t.getPpassword());
		int index = psmt.executeUpdate();
		DBUtils.closeResource(conn, psmt);
		return index > 0;
	}
	//王俊程

	@Override
	public void deleteById(int id) throws SQLException {
		Connection conn = DBUtils.getConnection();
		String sql = "delete from regist where pid = ?";
		PreparedStatement psmt = conn.prepareStatement(sql);
		psmt.setInt(1, id);
		psmt.executeUpdate();
		DBUtils.closeResource(conn, psmt);
	}
	//王俊程

	@Override
	public void update(Regist t) throws SQLException {

		Connection conn = DBUtils.getConnection();

		String sql = "update regist set pid=?, pname=?, pmoblie=?, ppassword=?";
		PreparedStatement psmt = conn.prepareStatement(sql);
		psmt.setInt(1, t.getPid());
		psmt.setString(2, t.getPname());
		psmt.setInt(3, t.getPmoblie());
		psmt.setString(4, t.getPpassword());
		psmt.executeUpdate();
		DBUtils.closeResource(conn, psmt);

	}
	//王俊程
	
	@Override
	public Regist queryByid(int id) throws SQLException {
		Connection conn = DBUtils.getConnection();
		String sql = "select * from regist where pid = ?";
		PreparedStatement psmt = conn.prepareStatement(sql);
		psmt.setInt(1, id);
		ResultSet rs = psmt.executeQuery();
		Regist user = null;
		if(rs.next()) {
			int pid = rs.getInt("pid");
			String name = rs.getString("pname");
			int moblie = rs.getInt("pmoblie");
			String password = rs.getString("ppassword");
			user = new Regist(pid, name, moblie, password);
		}

		DBUtils.closeResource(conn, psmt, rs);
		
		return user;
	}


	@Override
	public List<Regist> queryAll() throws SQLException {
		// TODO Auto-generated method stub
		return null;
	}
	
	//常开瑞
	@Override
	public Regist queryByNameAndPsw(String name, String password) throws SQLException {
		Connection conn = DBUtils.getConnection();
		String sql = "select * from regist where pname=? and ppassword = ?";
		PreparedStatement psmt = conn.prepareStatement(sql);
		psmt.setString(1, name);
		psmt.setString(2, password);
		ResultSet rs = psmt.executeQuery();
		Regist regist = null;
		if (rs.next()) {
			// 取数据
			int pid = rs.getInt("pid");
			String pname = rs.getString("pname");
			int pmoblie = rs.getInt("pmoblie");
			String ppassword = rs.getString("ppassword");
		
			regist = new Regist(pid, pname, pmoblie, ppassword);
		}

		// ④ 关闭资源
		DBUtils.closeResource(conn, psmt, rs);

		return regist;
	}

	@Override
	public Regist queryByName(String name) throws SQLException {
		// TODO Auto-generated method stub
		return null;
	}

}
